///These series of functions will accept ESRI Geometry Features, and return KML.
///Make sure you care calling the ESRI JS API in your source document so we can recognize the ESRI JS Geometry Objects

function esrigeomtokml(esrigeom, zvalue, altitudemode, tesselate, extrude){
 //this function accepts an ESRI geometry, and optionally allows you to provide a Z value.

var geomtype = esri.geometry.getJsonType(esrigeom);


	if (geomtype == 'esriGeometryPolygon' ){
	//If it is an ESRI Polygon, return a KML Polygon
		 var polygon = ge.createPolygon('');
		 
		 for (var z=0; z<esrigeom.rings.length; z++) {
		 
		 	if (z == 0){
		 		var outerBoundary = ge.createLinearRing('');
		 		var coords = outerBoundary.getCoordinates();
		 		//confirm(esrigeom.rings[z].length);
		 			for (i=0; i<esrigeom.rings[z].length; i++){
		 			
		 			coords.pushLatLngAlt(esrigeom.getPoint(z,i).y, esrigeom.getPoint(z,i).x, zvalue);
		 			
		 			}
		 		 //confirm('pushed '+ esrigeom.rings[z].length + ' coords');
		 		polygon.setOuterBoundary(outerBoundary);

		 	}else{
			 	var innerBoundary = ge.createLinearRing('');
				coords = innerBoundary.getCoordinates();
				
					for (i=0; i<esrigeom.rings[z].length; i++){
		 			
		 			coords.pushLatLngAlt(esrigeom.getPoint(z,i).y, esrigeom.getPoint(z,i).x, zvalue);
		 			
		 			}
				
				polygon.getInnerBoundaries().appendChild(innerBoundary);

		 	}
		 
	
		 }
		 
		 
		 polygon.setAltitudeMode(altitudemode);
		if (tesselate == 'true'){
		 	polygon.setTesselate(true);
		}
			
		if (extrude == 'true'){
		 	polygon.setExtrude(true);
		}
		 return polygon;

	}
}